import is.glif.fenius.ws.simple.ReservationParams;

import javax.xml.datatype.XMLGregorianCalendar;

import java.util.ArrayList;
import java.util.List;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class FeniusSimpleList extends HttpServlet {
    public static final long serialVersionUID = 01010101L;
    FeniusSimpleClient client = null;

    static {
	TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    }

    public FeniusSimpleList() 
    {
	client = new FeniusSimpleClient();
    }

    public static synchronized String getString(Calendar c) {
	if (c == null) return null;

	String s = (c.get(Calendar.MONTH)+1)
	    + "/" + c.get(Calendar.DAY_OF_MONTH)
	    + " " + c.get(Calendar.HOUR_OF_DAY)
	    + ":" + c.get(Calendar.MINUTE)
	    + ":" + c.get(Calendar.SECOND);
	return s;
    }

    public static Calendar getCalendar()
    {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(new Date());
	calendar.set(Calendar.MILLISECOND, 0);
	return calendar;
    }

    private String head1 =
	"<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"><script type=\"text/javascript\"><!--\n function refresh() { location.href=\"/servlets/FeniusSimpleList\";} \n --> </script> <title>Fenius/GNS-WSIv3 Reservation Monitor</title><body onload=\"setTimeout('refresh()', ";
    private String head2 = ");\">";
    private String tail = "</body></html>\n";
    private int refresh_time = 5;

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
	System.err.println("=========== doGet list start now\n");
	List<ReservationParams> rps = client.doList();
	String s = head1 + (refresh_time * 1000) + head2;
	Calendar c = getCalendar();

	s += "<H1 align=center>Fenius/GNS-WSIv3 Reservation Monitor</H1>\n";
	s += "<table width=\"96%\" align=\"center\" border=\"1\" cellpadding=\"5\"><caption><div align=\"right\">";
	s += getString(c);
	s += "</div></caption>";
	s += "<tr bgcolor=\"#C0C0C0\"><th>Reservation<BR>ID</th><th>Terminal<BR>Point</th><th>Terminal<BR>Point</th><th>Activate<BR>Time</th><th>Duration<BR>Time</th><th>BW<BR>(Mbps)</th><th>Status</th></tr>\n";

	if (rps != null && rps.size() != 0) {
	    int size = rps.size() - 1;
	    int i = 10;
	    boolean color = false;

	    while (i >= 0 && size >= 0) {
		ReservationParams rp = rps.get(size);

		if (color) {
		    s += "<tr bgcolor=\"#00FFFF\">";
		    color = false;
		} else {
		    s += "<tr>";
		    color = true;
		}

		s += "<td>" + rp.getReservationId() + "</td>";
		s += "<td>" + rp.getSrcEndpoint() + "</td>";
		s += "<td>" + rp.getDstEndpoint() + "</td>";
		XMLGregorianCalendar gc = rp.getActivationTime();
		s += "<td>" + gc.toString() + "</td>";
		/*
		String gcs = "" + 
		    gc.getYear() + "-" + gc.getMonth() + "-" +
		    gc.getDay() + " " + gc.getHour() + ":" +
		    gc.getMinute() + ":" + gc.getSecond();
		s += "<td>" + gcs + "</td>";
		*/
		s += "<td>" + rp.getDuration() + "</td>";
		s += "<td>" + rp.getBandwidth()/1000000 + "</td>";
		String status = rp.getStatus();
		s += "<td>" + status + "</td>";
		s += "</tr>\n";

		i --; 
		size --;
	    }
	}
	s += "</table>\n";
	s += tail;

	response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        out.println(s);
	System.err.println("=========== doGet list end \n");
    }

    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        doGet(request, response);
    }
}
